package com.action;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.lang.reflect.Method;
 
public class RPCClientImp  implements RPCClient{
	
	
	/*
	 * @SuppressWarnings("unchecked") public static <T> T getRemoteProxyObj(final
	 * Class<?> serviceInterface, final InetSocketAddress addr) { //
	 * 1.将本地的接口调用转换成JDK的动态代理，在动态代理中实现接口的远程调用 return (T)
	 * Proxy.newProxyInstance(serviceInterface.getClassLoader(), new Class<?>[] {
	 * serviceInterface }, new InvocationHandler() { public Object invoke(Object
	 * proxy, Method method, Object[] args) throws Throwable { Socket socket = null;
	 * ObjectOutputStream output = null; ObjectInputStream input = null; try {
	 * 
	 * // 2.创建Socket客户端，根据指定端口连接远程服务端 socket = new Socket(); socket.connect(addr);
	 * 
	 * // 3.将远程服务调用所需的接口类、方法名、参数列表等序列化后发送给服务端 output = new
	 * ObjectOutputStream(socket.getOutputStream()); //
	 * output.writeUTF(serviceInterface.getName()); //
	 * output.writeUTF(method.getName()); //
	 * output.writeObject(method.getParameterTypes()); output.writeObject(args);
	 * 
	 * // 4.同步阻塞等待服务器返回应答，获取应答后返回 input = new
	 * ObjectInputStream(socket.getInputStream()); return input.readObject(); }
	 * finally { if (socket != null) socket.close(); if (output != null)
	 * output.close(); if (input != null) input.close(); } } }); }
	 */
	@Override
    public  Object math(TestDTO dto,final InetSocketAddress addr)  {
             Socket socket = null;
             ObjectOutputStream output = null;
             ObjectInputStream input = null;
             try {
             	
                 // 2.创建Socket客户端，根据指定端口连接远程服务端
                 socket = new Socket();
                 socket.connect(addr);

                 // 3.参数序列化后发送给服务端
                 output = new ObjectOutputStream(socket.getOutputStream());
                 output.writeObject(dto);
          		socket.shutdownOutput();

                 // 4.同步阻塞等待服务器返回应答，获取应答后返回
                 input = new ObjectInputStream(socket.getInputStream());
                 return input.readObject();
             } catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} finally {
                 if (socket != null)
					try {
						socket.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
                 if (output != null)
					try {
						output.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
                 if (input != null)
					try {
						input.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
             }
			return null;
	}

	@Override
	public Object math2(TestDTO dto, InetSocketAddress addr) {
		 Socket socket = null;
         ObjectOutputStream output = null;
         ObjectInputStream input = null;
         try {
         	
             // 1.创建Socket客户端，根据指定端口连接远程服务端
             socket = new Socket();
             socket.connect(addr);

             // 2.参数序列化后发送给服务端
           //protoc序列化
     		Math2DTO dtoBuild = Math2DTO.newBuilder().setX(2).setY(3).build();
     		byte[] bytes = dtoBuild.toByteArray();
     		dtoBuild.writeTo(socket.getOutputStream());
     		socket.shutdownOutput();
             // 3.同步阻塞等待服务器返回应答，获取应答后返回
             input = new ObjectInputStream(socket.getInputStream());
             return input.readObject();
             
         } catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
             if (socket != null)
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
             if (output != null)
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
             if (input != null)
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
         }
		return null;
	}


	


}
